define(function () {
    'use strict';

    function ctrl($scope,$state, $ionicLoading,LocalCacheService, CommonService,FinancingService) {
        function init() {
            $scope.user = LocalCacheService.getUser();
            $scope.jurisdictions=[{
                isOpen:0,
                date:new Date(),
                href:'#/tab/account-easyFinancingOpen'
            },{
                isOpen:0,
                date:'',
                href:'#/tab/account-easyFinancingOpen'
            }];

            var params = {client_id: $scope.user.client_id};
            $ionicLoading.show();
            FinancingService.getUserAuthInfo(params).then(function (data) {
                $ionicLoading.hide();
                if (data.error_no== '0') {
                    if(data.accounts.length>0){
                        for (var i = 0; i < data.accounts.length; i++) {
                            var account = data.accounts[i];
                            if(account.opened=='true'){//已开通
                                $scope.jurisdictions[i].isOpen=2;
                                $scope.jurisdictions[i].date=account.open_time;
                                $scope.jurisdictions[i].href='#/tab/account-easyFinancingAgreement/0';
                            }else if(account.open_status == 'true'){//开通中
                                $scope.jurisdictions[i].isOpen=1;
                                $scope.jurisdictions[i].date=account.open_time;
                            }else{
                                $scope.jurisdictions[i].date=account.open_time;
                                if(account.open_time == '--'){//未开通
                                    $scope.jurisdictions[i].isOpen=0
                                }else{//失败(是否需要显示时间)
                                    $scope.jurisdictions[i].isOpen=0
                                }
                                $scope.jurisdictions[i].href='#/tab/account-easyFinancingOpen';
                            }
                        }
                    }
                }
            });
        }

        $scope.$on('$ionicView.loaded', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$state', '$ionicLoading', 'LocalCacheService', 'CommonService', 'FinancingService'];
    return ctrl;
});
